<?php

namespace common\models;
use Yii;
use yii\base\Model;

/**
 * This is the model class for table "user".
 *
 * @property int $id
 * @property string $name 名称
 * @property string $password 密码
 * @property string $phone 手机号码
 * @property string $site 地址
 * @property string $head_img 用户头像
 * @property string $create_time 创建时间
 * @property string $last_login_time 最后登录时间
 * @property string $last_login_ip 最后登录ip地址
 * @property int $status 状态
 */
class User extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */

//    public $imageFile;

    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
//            [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
            ['name', 'trim'],
            ['name', 'required','message' => '请输入用户名'],
            ['name', 'string', 'min' => 2, 'max' => 255],
            ['password', 'required','message' => '请输入密码,最少6位'],
            ['password', 'string', 'min' => 6],
            ['phone', 'required','message' => '请输入正确的手机号'],
            ['phone', 'string', 'max' => 11,'min'=>11],
            ['phone', 'unique', 'message' => '该手机号已存在'],
            [['create_time', 'last_login_time', 'last_login_ip'], 'safe'],
            [['status'], 'integer'],
            [['name', 'password'], 'string', 'max' => 100],
            [['phone'], 'string', 'max' => 11],
            [['site'], 'string', 'max' => 255],
        ];
    }

    public function upload()
    {
        if($this->imageFile){
            $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
            return true;
        }else{
            return false;
        }
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '用户名称',
            'password' => '密码',
            'phone' => '手机',
            'site' => '住址',
            'imageFile' => '头像',
            'create_time' => '创建时间',
            'last_login_time' => '最后登录时间',
            'last_login_ip' => '最后登录ip',
            'status' => '状态',
        ];
    }
}
